﻿using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;

namespace TowerDefence_Zombie
{
    /// <summary>
    /// An area suitable for placing a single tower
    /// </summary>
    [RequireComponent(typeof(Collider))]
    public class SingleTowerPlacementArea : MonoBehaviour, IPlacementArea
    {
        [SerializeField] private int plcaementAreaId;
        [SerializeField] private Collider triggerCollider;
        [SerializeField] private GameObject placementLockObj;
        [SerializeField] private GameObject highlightObj;

        [SerializeField,Title("底座Mesh Render")] private MeshRenderer meshRenderer;

        [SerializeField,Title("默认底座材质")] private Material defaultMaterial;
        [SerializeField, Title("机枪兵底座材质")] private Material gunnerMaterial;
        [SerializeField,Title("冰人底座材质")] private Material iceMaterial;
        [SerializeField,Title("老白底座材质")] private Material oldManMaterial;
        [SerializeField,Title("钢铁侠底座材质")]private Material steveMaterial;

        bool m_IsLock;
        bool m_IsOccupied;

        public int AreaId
        {
            get
            {
                return plcaementAreaId;
            }
        }

        public bool IsOccupied
        {
            get
            {
                return m_IsOccupied;
            }
        }

        public bool IsLock
        {
            get
            {
                return m_IsLock;
            }
        }

        private void Awake()
        {
            m_IsOccupied = false;
            SetLockState(false, true);
        }

        public void SetLockState(bool state, bool force = false)
        {
            if (force == false && m_IsLock == state) return;

            m_IsLock = state;
            triggerCollider.enabled = !state;
            placementLockObj.SetActive(state);
        }

        public void SetHighlightState(bool state)
        {
            highlightObj.SetActive(state);
        }

        public void Occupy(IntVector2 gridPos, IntVector2 size, int towerId)
        {
            m_IsOccupied = true;
            if (towerId == 107)
                meshRenderer.material = gunnerMaterial;
            else if (towerId == 108)
                meshRenderer.material = iceMaterial;
            else if (towerId == 109)
                meshRenderer.material = oldManMaterial;
            else if (towerId == 110)
                meshRenderer.material = steveMaterial;
            else
                meshRenderer.material = defaultMaterial;
        }

        public void Clear(IntVector2 gridPos, IntVector2 size)
        {
            m_IsOccupied = false;
            if(meshRenderer!= null)
                meshRenderer.material = defaultMaterial;
        }

        public IntVector2 WorldToGrid(Vector3 worldPosition, IntVector2 sizeOffset)
        {
            return new IntVector2(0, 0);
        }

        public Vector3 GridToWorld(IntVector2 gridPosition, IntVector2 sizeOffset)
        {
            return transform.position;
        }

        public TowerFitStatus Fits(IntVector2 gridPos, IntVector2 size)
        {
            if (m_IsLock) return TowerFitStatus.Lock;
            return m_IsOccupied ? TowerFitStatus.Overlaps : TowerFitStatus.Fits;
        }

#if UNITY_EDITOR
        /// <summary>
        /// Draw the spot as a smalls phere in the scene view.
        /// </summary>
        void OnDrawGizmos()
        {
            Color prevCol = Gizmos.color;
            Gizmos.color = Color.cyan;

            Matrix4x4 originalMatrix = Gizmos.matrix;
            Gizmos.matrix = transform.localToWorldMatrix;

            Gizmos.DrawWireSphere(Vector3.zero, 1);

            Gizmos.matrix = originalMatrix;
            Gizmos.color = prevCol;

            // Draw icon too
            Gizmos.DrawIcon(transform.position + Vector3.up, "build_zone.png", true);
        }
#endif
    }
}